package com.kaili.db.service.impl;

import com.kaili.pojo.entity.HsBasicSeq;
import com.kaili.db.mapper.HsBasicSeqMapper;
import com.kaili.db.service.IHsBasicSeqService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-05-08
 */
@Service
public class HsBasicSeqServiceImpl extends ServiceImpl<HsBasicSeqMapper, HsBasicSeq> implements IHsBasicSeqService {
    /**
     * 根据类型获取序列号
     * @param type
     * @return
     */
    @Override
    public synchronized String seqByType(String type) {
        Map<String ,Object> map = new HashMap();
        map.put("type",type);
        List<HsBasicSeq> lst = baseMapper.selectByMap(map);
        HsBasicSeq cur = null;
        Date nows = DateUtils.getNowDateShort();
        if(lst==null||lst.size()==0){
            cur = new HsBasicSeq();
            cur.setType(type);
            cur.setSeq(0);
            cur.setCurrent(nows);
            baseMapper.insert(cur);
        }else{
            cur = lst.get(0);
            if(cur.getCurrent().getTime()<nows.getTime()){
                cur.setCurrent(nows);
                cur.setSeq(0);
            }else{
                cur.setSeq(cur.getSeq()+1);
            }
            baseMapper.updateById(cur);
        }
        return DateUtils.dateToStrS(cur.getCurrent())+String.format("%05d",cur.getSeq());
    }

    @Override
    public String seqByTypeWithType(String type) {
        return type+seqByType(type);
    }
}